<template>
 <!-- style="padding: 10px;width: 100%" -->
<div  class="bc">
  <el-row >
    <!-- style="padding: 10px" -->
  </el-row>
  <div  style="margin: 10px 0">

    <el-input v-model="search" placeholder="请输入内容" style="width: 30%" clearable @clear="clear"></el-input>  <el-button @click="SearchList">查询</el-button>
  </div>

  <el-table
      :data="userList"
      border
      style="width: 100%;"
      stripe
  >
    <el-table-column
        prop="id"
        label="ID"
        >
    </el-table-column>
    <el-table-column
        prop="username"
        label="用户名"
        >
    </el-table-column>
       <el-table-column
        prop="score"
        label="发帖积分"
        width="110px"
        sortable

        >
    </el-table-column>
    <el-table-column
        prop="createTime"
        label="账号创建时间">
    </el-table-column>
        <el-table-column
        label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">更多详情</el-button>
          <el-button type="danger" size="small" @click="handleZchange(scope.row)">撤销管理员</el-button>
        </template>
    </el-table-column>

     <!-- <el-table-column
        prop="status"
        label="当前状态">
         <template #default="scope">
            <el-switch
               @change="switchChange(scope.row)"
               style="display: block"
               v-model="scope.row.status"
               active-color="#13ce66"
               inactive-color="#ff4949"
               active-text="正常"
               inactive-text="封禁">

             </el-switch>
          </template>
    </el-table-column> -->

  </el-table>

  <div>
    <div>
      <el-pagination
        :currentPage="current"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    >
    </el-pagination>

<el-dialog
  title="用户详情"
  :visible.sync="dialogVisible"
  width="30%">
  <el-form :model="Userform" label-width="80px">
       <el-form-item label="用户名">
         <el-input v-model="Userform.username"></el-input>
       </el-form-item>
       <el-form-item label="昵称">
         <el-input v-model="Userform.alias"></el-input>
       </el-form-item>
        <el-form-item label="职业">
         <el-input v-model="Userform.bio"></el-input>
       </el-form-item>
         <el-form-item label="密码">
         <el-input v-model="Userform.password"></el-input>
       </el-form-item>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>

    </div>
  </div>
</div>
</template>

<script>
import { getallSuper, update } from '@/api/user'
import { applyAd } from '@/api/applyAD'
// import { getInfo, update } from '@/api/user'
export default {
  data () {
    return {
      search: '',
      dialogVisible: false,
      Userform: {},
      userList: [],
      formWork: {
        // username: '',
        // applySession: '',
        // applydata: ''
      },
      current: 1,
      size: 10,
      total: 0

    }
  },
  created () {
    this.load()
    window.localStorage.setItem('changNum', 0)
  },
  methods: {
    load () {
      getallSuper(this.current, this.size, this.search).then((response) => {
        const { data } = response
        this.current = data.current
        this.total = data.total
        this.pageSize = data.size
        this.userList = data.records
        // this.Userform = data
        console.log(data)
      })
    },
    SearchList () {
      getallSuper(1, this.size, this.search).then((response) => {
        const { data } = response
        this.current = data.current
        this.total = data.total
        this.pageSize = data.size
        this.userList = data.records
        // this.Userform = data
        console.log(data)
      })
    },
    clearable () {
      console.log(123)
      this.load()
    },
    handleSizeChange (size) {
      this.size = size
      this.load()
    },
    handleCurrentChange (pageNo) {
      this.current = pageNo
      this.load()
    },
    handleEdit (row) {
      this.Userform = JSON.parse(JSON.stringify(row)) // 表单的对象数据是跟form绑定的 深拷贝防止点击取消之后发生改变
      this.dialogVisible = true
    },
    clear () {
      this.load()
    },
    handleZchange (item) {
      console.log(item)
      // console.log('当前状态是' + bollen.username + bollen.status)

      // this.user.avatar = res.data
      // update(user).then(res => {
      //   this.$message.success('已经撤销了他的超级管理员')
      //   // this.load()
      //   // window.location.reload()
      // })
      this.$confirm('是否要撤销该用户的管理员?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const user = item
        user.active = false
        console.log(user)
        // const formWork = {}
        this.formWork.username = item.username
        this.formWork.userId = item.id
        this.formWork.role = 3
        this.formWork.status = 0
        this.formWork.applydata = new Date()
        try {
          update(user).then(value => {
            applyAd(this.formWork).then(res => {
            })
            this.$message.success('撤销成功')
            this.load()
          })
        } catch (e) {
          this.$message.error('撤销失败')
        }
      })
        .catch(() => {
          // return true
          this.$message({
            type: 'info',
            message: '已取消撤销'
          })
        })
    }

  },
  mounted () {}
}
</script>

<style lang="less" scoped>
.bc{
  padding: 10px;
  background-color: #fff;
  height: 100vh;
}
.el-table{
  // border: 3px solid rgb(177, 177, 177);
}
</style>
